package org.example.zhihuan.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Result {
    private int code;
    private String message;
    private Object value;

    public static final int NOT_ALLOW = 501;
    public static final int USER_BAN = 502;
    public static final int PARAMETER_WRONG = 321;      //参数不正确
    public static final int PASS_WRONG = 322;           //密码不正确


    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getValue() {
        return value;
    }

    public static Result ok(Object value) {
        return new Result(200, "成功", value);
    }

    public static Result fail(int code, String message, Object value) {
        return new Result(code, message, value);
    }

    public Result(int code, String message, Object value) {
        this.code = code;
        this.message = message;
        this.value = value;
    }

    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "";
    }

}
